<?php

/**
 * Sample Hoathis_Kml
 *
 * @author      Grégory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Grégory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @package     Hoathis_Kml
 */

error_reporting(E_ALL);

set_include_path('./../'            	 . PATH_SEPARATOR .
                 './../Framework/Core'   . PATH_SEPARATOR .
                 get_include_path());

require_once 'Framework.php';

importModule('Kml.~');
importModule('Kml.Object');
importModule('Kml.Feature.Document');
importModule('Kml.Feature.Folder');
importModule('Kml.Feature.PlaceMark');
importModule('Kml.Geometry.LineString');
importModule('Kml.Geometry.Point');
importModule('Kml.Geometry.Polygon');
importModule('Kml.StyleSelector.Style');
importModule('Kml.TimePrimitive.TimeStamp');

/**
 * Initialisation du fichier Kml
 */

// Instance Kml
$kml = new Hoathis_Kml('Régate 2009');

// Ajout de fichiers externes
$kml->addFile('images/navint.png', 'images/navint.png');
$kml->addFile('images/icone.png', 'images/icone.png');
$kml->addFile('images/small.png', 'images/small.png');
$kml->addFile('images/fish.png', 'images/fish.png');
$kml->addFile('images/port.png', 'images/port.png');

/**
 * Initialisation du DOCUMENT Kml
 */

// Instance DOCUMENT
$document = new Hoathis_Kml_Feature_Document('Régate', 'Régate 2009');

// Ajout des styles personnalisés
$style = new Hoathis_Kml_StyleSelector_Style('boatStyle');
$style->setIconStyle('images/fish.png', 'ffffffff', 'normal', 1);
$style->setLineStyle('ffffffff', 'normal', 2);
$document->addStyle($style);

$style = new Hoathis_Kml_StyleSelector_Style('navintStyle');
$style->setIconStyle('images/navint.png', 'ffffffff', 'normal', 1);
$style->setLineStyle('ff0000ff', 'normal', 3);
$document->addStyle($style);

$style = new Hoathis_Kml_StyleSelector_Style('plotStyle');
$style->setIconStyle('images/small.png', 'ff00ff00', 'normal', 0.2);
$document->addStyle($style);

$style = new Hoathis_Kml_StyleSelector_Style('portStyle');
$style->setIconStyle('images/port.png');
$document->addStyle($style);

$style = new Hoathis_Kml_StyleSelector_Style('zoneStyle');
$style->setPolyStyle('66ffffff');
$document->addStyle($style);

//----

/**
 * Création des dossiers et repères
 */

// Création du dossier des bateaux
$boatListFolder = new Hoathis_Kml_Feature_Folder('', 'Voiliers');

	// Création du dossier du bateau IMX40 TooZen
	$boatFolder = new Hoathis_Kml_Feature_Folder('', 'IMX40');

		// Ajout des information sur ce bateau
		$boatDetail = new Hoathis_Kml_Feature_PlaceMark('', 'IMX40, un grand voilier !', 'Voilier', true);
		$boatDetail->setGeometry(new Hoathis_Kml_Geometry_Point(-2.808819730039009, 47.33400766188341, 0));
		$boatDetail->setStyleUrl('#boatStyle');

		// Ajout d'une bulle sur ce bateau
		$style = new Hoathis_Kml_StyleSelector_Style();
		$style->setBalloonStyle ('Une jolie description pour ce bateau.');
		$boatDetail->addStyle($style);
		$boatFolder->addFeature($boatDetail);

        //----

		// Ajout du tracé des itinéraires
		$boatTrace = new Hoathis_Kml_Feature_PlaceMark(null, 'Itinéraires', '', true);
		$boatTrace->setGeometry (new Hoathis_Kml_Geometry_LineString( Array (
		                           array (-2.889049810017922, 47.54450692577759, 0),
		                           array (-3.072739292406215, 47.4120104344829, 0),
		                           array (-2.896105675032522, 47.30075254105016, 0),
		                           array (-2.808819730039009, 47.33400766188341, 0)
		                        ), true, '', true)
		                     );

		// Ajout de l'interval
		$boatTrace->setTimePrimitive(new Hoathis_Kml_TimePrimitive_TimeStamp('','2009-03-26','2009-03-29'));
		$boatTrace->setStyleUrl('#boatStyle');
		$boatFolder->addFeature($boatTrace);

        //----

		// Création d'un dossier de l'historique des positions
		$boatHistoFolder = new Hoathis_Kml_Feature_Folder('', 'Historique des positions');

			$boatFollow = new Hoathis_Kml_Feature_PlaceMark('', '2009-03-26', '', true);
			$boatFollow->setGeometry(new Hoathis_Kml_Geometry_Point(-2.889049810017922, 47.54450692577759, 0));
			$boatFollow->setStyleUrl('#plotStyle');
			$boatFollow->setTimePrimitive(new Hoathis_Kml_TimePrimitive_TimeStamp('','2009-03-26'));
			$boatHistoFolder->addFeature($boatFollow);

			$boatFollow = new Hoathis_Kml_Feature_PlaceMark('', '2009-03-27', '', true);
			$boatFollow->setGeometry(new Hoathis_Kml_Geometry_Point(-3.072739292406215, 47.4120104344829, 0));
			$boatFollow->setStyleUrl('#plotStyle');
			$boatFollow->setTimePrimitive(new Hoathis_Kml_TimePrimitive_TimeStamp('','2009-03-27'));
			$boatHistoFolder->addFeature($boatFollow);

			$boatFollow = new Hoathis_Kml_Feature_PlaceMark('', '2009-03-28', '', true);
			$boatFollow->setGeometry(new Hoathis_Kml_Geometry_Point(-2.896105675032522, 47.30075254105016, 0));
			$boatFollow->setStyleUrl('#plotStyle');
			$boatFollow->setTimePrimitive(new Hoathis_Kml_TimePrimitive_TimeStamp('','2009-03-28'));
			$boatHistoFolder->addFeature($boatFollow);

			$boatFollow = new Hoathis_Kml_Feature_PlaceMark('', '2009-03-29', '', true);
			$boatFollow->setGeometry(new Hoathis_Kml_Geometry_Point(-2.808819730039009, 47.33400766188341, 0));
			$boatFollow->setStyleUrl('#plotStyle');
			$boatFollow->setTimePrimitive(new Hoathis_Kml_TimePrimitive_TimeStamp('','2009-03-29'));
			$boatHistoFolder->addFeature($boatFollow);

		// Insertion des positions dans le dossier des positions
		$boatFolder->addFeature($boatHistoFolder);

        //----

		// Création du dossier des zones
		$areaFolder = new Hoathis_Kml_Feature_Folder('', 'Zones');

			// Ajout de la zone de périmètre
			$perimetre = new Hoathis_Kml_Feature_PlaceMark('', 'Périmètre de course');
			$perimetre->setGeometry (new Hoathis_Kml_Geometry_Polygon( Array (
									   array (-2.889049810017922, 47.54450692577759, 0),
									   array (-3.072739292406215, 47.4120104344829, 0),
			                           array (-2.896105675032522, 47.30075254105016, 0),
			                           array (-2.808819730039009, 47.33400766188341, 0)
			                        ), true, '', true)
			                     );
			$perimetre->setStyleUrl('#zoneStyle');
			$areaFolder->addFeature($perimetre);

		// Insertion du bateau dans le dossier des bateaux
		$boatFolder->addFeature($areaFolder);

	// Insertion du bateau dans le dossier des bateaux
	$boatListFolder->addFeature($boatFolder);

// Insertion du dossier des voiliers dans le DOCUMENT
$document->addFeature($boatListFolder);

//----

// Création du dossier des ports
$portFolder = new Hoathis_Kml_Feature_Folder('', 'Ports');

	// Ajout d'un repère port
	$port = new Hoathis_Kml_Feature_PlaceMark('', 'Port crouesty');
	$port->setGeometry(new Hoathis_Kml_Geometry_Point(-2.889049810017922, 47.54450692577759, 0));
	$port->setStyleUrl('#portStyle');
	$portFolder->addFeature($port);

	// Ajout d'un repère port
	$port = new Hoathis_Kml_Feature_PlaceMark('', 'Port houat');
	$port->setGeometry(new Hoathis_Kml_Geometry_Point(-2.955032097500991, 47.39327129466578, 0));
	$port->setStyleUrl('#portStyle');
	$portFolder->addFeature($port);

	// Ajout d'un repère port
	$port = new Hoathis_Kml_Feature_PlaceMark('', 'Port hoédic');
	$port->setGeometry(new Hoathis_Kml_Geometry_Point(-2.871737655258519, 47.34381717722765, 0));
	$port->setStyleUrl('#portStyle');
	$portFolder->addFeature($port);

// Insertion du dossier des ports dans le DOCUMENT
$document->addFeature($portFolder);

//----

//Insertion du DOCUMENT dans le fichier Kml du répertoire
$kml->setFeature($document);

/**
 * Formats de sortie du résultat
 */

// Affichage sous forme de chaine
//echo '<pre>';
//echo htmlspecialchars($kml->output('S'));
//echo '</pre>';

// Ouverture du fichier directement depuis le navigateur
//echo $kml->output('A');

// Format KMZ
if ($kml->output('F', 'demo_kml.kml'))
	echo 'Fichier généré !';

// Format KMZ (format compréssé)
//if ($kml->output('Z'))
//	echo 'Fichier généré !';

// Fin. ^^

?>